package com.kzw.oa.comm.service;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kzw.core.service.hibernate.DefaultEntityManager;
import com.kzw.misc.utils.ContextUtil;
import com.kzw.oa.comm.model.Log;
import com.kzw.oa.comm.type.LogType;
import com.kzw.oa.system.model.AppUser;

@Service
@Transactional
public class LogService extends DefaultEntityManager<Log, String> {
	
	@Autowired
	private  HttpServletRequest request;

	public void log(LogType type, HttpServletRequest request, String module) {
		AppUser user = ContextUtil.getCurrentUser();
		Log log = new Log();
		log.setLoginName(user.getUsername());
		log.setRealName(user.getFullname());
		// 一个用户多个部门 ？
		log.setDeptName(user.getDept().getDepName());
		log.setIp(request.getRemoteAddr());
		log.setOpType(type.getName());
		String client = request.getHeader("User-Agent");
		log.setClient(client);
		log.setModule(module);
		saveOrUpdate(log);
	}
	
	public void log(String type, HttpServletRequest request, String module) {
		AppUser user = ContextUtil.getCurrentUser();
		Log log = new Log();
		log.setLoginName(user.getUsername());
		log.setRealName(user.getFullname());
		// 一个用户多个部门 ？
		log.setDeptName(user.getDept().getDepName());
		log.setIp(request.getRemoteAddr());
		log.setOpType(type);
		String client = request.getHeader("User-Agent");
		log.setClient(client);
		log.setModule(module);
		saveOrUpdate(log);
	}
	
	public void login(AppUser user) {
		Log log = new Log();
		log.setLoginName(user.getUsername());
		log.setRealName(user.getFullname());
		log.setDeptName(user.getDept().getDepName());		
		log.setIp(request.getRemoteAddr());
		log.setOpType("登陆");
		String client = request.getHeader("User-Agent");
		log.setClient(client);
		log.setModule("系统登陆");
		saveOrUpdate(log);
	}
	
	/**
	 * 日志记录
	 * */
	public void log(String module, String opt, String msg) {
		AppUser user = ContextUtil.getCurrentUser();
		Log log = new Log();
		log.setLoginName(user==null?"sys":user.getUsername());
		log.setRealName(user==null?"系统":user.getFullname());
		log.setDeptName(user==null?"系统":user.getDept().getDepName());		
		log.setOpType(opt);
		log.setModule(module);
		log.setMsg(msg);
		try {
			log.setIp(request.getRemoteAddr());
			String client = request.getHeader("User-Agent");
			log.setClient(client);
		} catch(Exception e) {
			// 定时器
		}
		saveOrUpdate(log);
	}

	public Log getLog(LogType type, String module, String message) {
		AppUser user = ContextUtil.getCurrentUser();
		Log log = new Log();
		log.setLoginName(user==null?"sys":user.getUsername());
		log.setRealName(user==null?"系统":user.getFullname());
		log.setDeptName(user==null?"系统":user.getDept().getDepName());
		log.setOpType(type.getName());
		log.setModule(module);
		log.setMsg(message);

		try {
			log.setIp(request.getRemoteAddr());
			String client = request.getHeader("User-Agent");
			log.setClient(client);
		} catch(Exception e) {
			// 定时器或系统操作，取不到request对象
		}
		return log;
	}

	/**
	 * 普通日志
	 * */
	public void info(LogType type, String module, String message) {
		Log log = getLog(type, module, message);
		log.setLevel(1);
		saveOrUpdate(log);
	}

	/**
	 * 警告日志
	 * */
	public void warn(LogType type, String module, String message) {
		Log log = getLog(type, module, message);
		log.setLevel(2);
		saveOrUpdate(log);
	}

	/**
	 * 严重日志
	 * */
	public void error(LogType type, String module, String message) {
		Log log = getLog(type, module, message);
		log.setLevel(3);
		saveOrUpdate(log);
	}
}
